<?php
//------------------------------------------------------------------------------
// Modification       :
//------------------------------------------------------------------------------
// Date(yyyy.mm.dd)   Developer                       Action
//------------------------------------------------------------------------------
// 2007.03.05         ThanhTX                      Created
//------------------------------------------------------------------------------
// 2007.06.08         ThanhTX                      Update
//------------------------------------------------------------------------------
// 2007.06.18         ThanhTX                      Update
//------------------------------------------------------------------------------
// 2007.06.20         ThanhTX                      Update
//------------------------------------------------------------------------------
// show menu

//==========================================================================
// Function    : showInfoForUser
// Creator     : ThanhTX 2007.09.05
// Details     : 
//==========================================================================
function showInfoForUser($arrayData_Info){
	if (!empty($arrayData_Info)) {   
		foreach  ($arrayData_Info as $row)
		{
			echo "<tr><td  class=\"menuLeft\" onmouseover=ChuotVao(this); onmouseout=ChuotRa(this); onClick=\"javascript: SearchMenuLeft(\"?modul=info&action=View&id=".$row["id"]."\"); OnclickSub(this);\" style=\"FONT-SIZE: 8pt\" ><font color=\"#000033\">&nbsp;&nbsp;".$row['tieu_de']."</font></td></tr>";
		}
	}
}
//==========================================================================
// Function    : showInfoForUser
// Creator     : ThanhTX 2007.09.05
// Details     : 
//==========================================================================
function showProductForUser($arrayData_Product){
	if (!empty($arrayData_Product)) {   
		foreach  ($arrayData_Product as $row)
		{
			echo '<tr><td>&nbsp;</td><td class="menuLeft" onmouseover=ChuotVao(this); onmouseout=ChuotRa(this); onClick="javascript: SearchMenuLeft(\'?modul=product&action=View&id='.$row["id"].'\'); OnclickSub(this);" style="FONT-SIZE: 8pt" ><font color="#000033">&nbsp;&nbsp;'.$row['tieu_de'].'</font></td></tr>';
		}
	}
}

function createOneMenu($url,$text,$on=false,$space=""){
	if($on) $class = "leftmenu_normal_on";
	else 	$class = "leftmenu_normal";
	?>		 	
	 <TR> 
	  <TD CLASS="<?=$class ?>" onclick=" menu_onclick('<?=$url?>')"  onMouseOut="menu_onmouseout(this)" onmouseover="menu_onmouseover(this)" >
		&nbsp;<?=$space?><img src="../images/TriRBlue.gif" width="9" height="9"/>
		<A CLASS="leftmenu" HREF="<?=$url?>" > <?=$text?></A>
	  </TD>
	 </TR>
	<?php
}


//function dinh dang ngay thang
function CM_dateTime($datetime){
	global $datetime;
	$formatdate=substr($datetime,8,2)."/".substr($datetime,5,2)."/".substr($datetime,0,4);
	return $formatdate;
}
function CM_getParameter($parameterName){
	global $_GET;
	global $_POST;
	$n			   = $_GET[$parameterName];
	if(empty($n)) $n = $_POST[$parameterName];
	return trim($n);
}
function CM_redirect($url){
		header("location: $url");
		exit();
}
function CM_gotoPage($url,$t=0){
	$str = ' <script language = "JavaScript" >
			location = "'.$url.'";
		</script>';
	if($t)return $str;
	echo $str;
}

function CM_showError(){
	global $arrayError;

	if (!$arrayError){
		return NULL;
	}
	echo '<div  style="background-color:#FF0000; color:#FFFFFF "> <center>  ';
	foreach ($arrayError as $error){
		echo " $error ";
	}
	echo '</center> </div>';
	unset($arrayError );
	global $arrayError ;
	$arrayError =  array();
}

function CM_showMessage(){
	global $arrayMessage;

	if (!$arrayMessage){
		return NULL;
	}
	echo '<div  style="background-color:#33CCFF; color:#FFFFFF ">  <center>  ';
	foreach ($arrayMessage as $message){
		echo " $message ";
	}
	echo '</center> </div>';
	unset($arrayMessage );
	global $arrayMessage ;
	$arrayMessage =  array();
}



function CM_countError(){
	global $arrayError;	
	return count($arrayError);
}


//layDuongDanThuMucMe
function CM_getDirParen(){
	$url = $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']) ;
	$str = substr($url,0,strrpos($url,'/'));
	return $str;
}
function  CM_upload($tenFileUpload,$duongDan,$duoiChoPhep='gif,bmp,jpg,GIF,BMP,JPG'){
	global $_FILES;
	global $HTTP_POST_VARS ;
	$duoi3 = substr($_FILES[$tenFileUpload]['name'],strlen($_FILES[$tenFileUpload]['name'])-3,3);
	// kiem tra
	if(! strstr($duoiChoPhep,$duoi3)) return false;
	if( move_uploaded_file($_FILES[$tenFileUpLoad],'abctempl')) return false;
	if(! copy($_FILES[$tenFileUpload]['tmp_name'],$duongDan)) return false;
	return  true;
}
function CM_saveFile($tenFile,$str,$mode='w'){
	$f = fopen($tenFile,$mode);
	if(!$f) return false;
	if(fwrite($f,$str)) {
		fclose($f);
		return true;
	}
	return false;
}
	function CM_form_post_back(){
		return "<form class='form' action='?" . htmlspecialchars($_SERVER['QUERY_STRING']) . "' method='post'>";
	}

function CM_createOption($listValues,$valueSelected=""){
		foreach  ($listValues as $row)
		{
			$id=$row[0];
			if($id==$valueSelected)
			{
				echo"<option value=\"$id\" selected>$row[1]</option>";
			}else{
				echo"<option value=\"$id\">$row[1]</option>";
			}
		} // end foreach  ($arrayData_ttDauTu as $row)
}
	function link_to($linktext, $controller, $action="index", $suffix=NULL, $html_attributes=""){
		$html = "<a href='?controller=$controller&amp;action=$action";

		if ($suffix!=NULL){
			$html .= htmlspecialchars("&$suffix");
		}

		$html .= "'";

		$html .= " $html_attributes ";

		$html .= ">$linktext</a>";

		return $html;
	}

	function link_to_confirm($linktext, $controller, $action="index", $suffix=NULL){
		$html = link_to($linktext, $controller, $action, $suffix, "onclick=\"return confirm('Are you sure?');\"");
		return $html;
	}

	function image_tag($name){
		return "<img src='/images/$name' alt='" . preg_replace("/\\..+/", "", $name) . "' />";
	}

	# Prints the errors in an object as a list
	function error_messages_for($object){
		global $$object;
		$obj = $$object;

		$html = "";

		$html .= "<ul class='errors'>";

		foreach ($obj->_errors as $e){
			$html .= "<li>$e</li>";
		}

		$html .= "</ul>";

		return $html;
	}

	function suppress_field_error(){
		global $_field_errors;

		$_field_errors = FALSE;
	}

	# Return the name of a field as should be inserted into the html
	function _field_name($object, $field){
		if (strpos($object, "[]")===FALSE){
			return $object . "[$field]";
		}else{
			$object = str_replace("[]", "", $object);

			global $$object;

			return $object . "[" . $$object->id . "][$field]";
		}
	}

	# Return the value of a field
	function _field_value($object, $field){
		$object = str_replace("[]", "", $object);

		global $$object;
		$obj = $$object;
		return @$obj->$field;
	}

	function check_box($object, $field, $html_attributes=""){
		$html = "<input type='checkbox' class='check' value='Yes' name='" . _field_name($object, $field) . "'";

		if (_field_value($object, $field)=="Yes"){
			$html .= " checked='checked' ";
		}

		$html .= $html_attributes;

		$html .= " />";

		$html .= error_message_for($object, $field);

		return $html;
	}

	function radio_button($object, $field, $value, $html_attributes=""){
		$html = "<input id='" . strtolower("$object-$field-$value") . "' type='radio' class='radio' value='" . $value . "' name='" . _field_name($object, $field) . "' ";

		if (_field_value($object, $field)==$value){
			$html .= "checked='checked'";
		}

		$html .= $html_attributes;

		$html .= " />";

		$html .= error_message_for($object, $field);

		return $html;
	}

	function text_field($object, $field, $html_attributes=""){
		$html = "<input type='text' value='" . _field_value($object, $field) . "' name='" . _field_name($object, $field) . "' ";
		$html .= $html_attributes;
		$html .= " />";

		$html .= error_message_for($object, $field);

		return $html;
	}

	function text_area($object, $field, $html_attributes=""){
		$html = "<textarea name='" . _field_name($object, $field) . "' ";
		$html .= $html_attributes;
		$html .= ">";
		$html .= _field_value($object, $field);
		$html .= "</textarea>";
		$html .= error_message_for($object, $field);
		return $html;
	}
	function hidden_field($object, $field, $html_attributes=""){
		$html = "<input type='hidden' value='" . _field_value($object, $field) . "' name='" . _field_name($object, $field) . "' ";
		$html .= $html_attributes;
		$html .= " />";

		return $html;
	}
	function password_field($object, $field, $html_attributes=""){
		$html = "<input type='password' name='" . _field_name($object, $field) . "' ";
		$html .= $html_attributes;
		$html .= " />";
		$html .= error_message_for($object, $field);
		return $html;
	}
	
	function select($object, $field, $values){
		$values[""]="";
		$html = "<select name='" . _field_name($object, $field) . "'>";
		foreach ($values as $key=>$value){
			$html .= "<option ";
			# If no keys are supplied - we use the value as the key
			if (is_numeric($key)){
				$key = $value;
			}
			if ($key==_field_value($object, $field)){
				$html .= "selected='selected'";
			}
			$html .= " value='$key'";

			$html .= ">$value</option>";
		}
		$html .= "</select>";
		$html .= error_message_for($object, $field);
		return $html;
	}
	

function to_money($string)
{
$Negative = 0;

if(preg_match("/^\-/",$string))
{
$Negative = 1;
$string = preg_replace("|\-|","",$string);
}

$string = preg_replace("|\,|","",$string);
$Full = split("[\.]",$string);
$Count = count($Full);
if($Count > 1)
{
$First = $Full[0];
$Second = $Full[1];
$NumCents = strlen($Second);
if($NumCents == 2)
{
}
else if($NumCents < 2)
{
$Second = $Second . "0";
}
else if($NumCents > 2)
{
$Temp = substr($Second,0,3);
$Rounded = round($Temp,-1);
$Second = substr($Rounded,0,2);
}
}
else
{
$First = $Full[0];
$Second = "00";
}
$length = strlen($First);
if( $length <= 3 )
{
//$string = $First . "." . $Second;
$string = $First;
if($Negative == 1)
{
$string = "-" . $string;
}
return $string;
}
else
{
$loop_count = intval( ( $length / 3 ) );
$section_length = -3;
for( $i = 0; $i < $loop_count; $i++ )
{
$sections[$i] = substr( $First, $section_length, 3 );
$section_length = $section_length - 3;
}
$stub = ( $length % 3 );
if( $stub != 0 )
{
$sections[$i] = substr( $First, 0, $stub );
}
$Done = implode( ",", array_reverse( $sections ) );
//$Done = $Done . "." . $Second;
$Done = $Done;
if($Negative == 1)
{
$Done = "-" . $Done;
}

return $Done;
}
}

function sub_str($string,$length){		
$substr=explode(" ",$string);
 for($i=0;$i<$length;$i++)	   {
 
	$str.=" ".$substr[$i];					
 }
  return $str;
}


function cut_string($str,$len,$more){
if ($str=="" || $str==NULL) return $str;
if (is_array($str)) return $str;
$str = trim($str);
if (strlen($str) <= $len) return $str;
$str = substr($str,0,$len);
if ($str != "") {
if (!substr_count($str," ")) {
if ($more) $str .= "...";
return $str;
}
while(strlen($str) && ($str[strlen($str)-1] != " ")) {
$str = substr($str,0,-1);
}
$str = substr($str,0,-1);
if ($more) $str .= "...";
}
return $str;
} 

?>